import { useStore as useUserStore } from "@/store/user";
import { authInterface } from "@/common/interface/auth.interface";
export const analysisRolesAndOr = (auth: authInterface): boolean => {
  const userStore = useUserStore();
  if (userStore.info.roles.includes("admin")) {
    return true;
  }
  const dealAuth: any = (e: any) => {
    // 字符串类型,判断权限
    if (typeof e === 'string') {
      return !e || userStore.info.roles.includes(e);
    }
    // 对象类型(单属性对象), 对象key可为and | or 
    else {
      // key为and时, value条件须全部符合条件 
      if (Object.keys(e)[0] === 'and') {
        return (Object.values(e)[0] as any[])?.every((x: any) => dealAuth(x));
      }
      // key为or时, value条件只需符合其中一项
      else {
        return (Object.values(e)[0] as any[])?.some((x: any) => dealAuth(x));
      }
    }
  }
  return dealAuth(auth);
}
export const analysisPermissionsAndOr = (auth: authInterface): boolean => {
  const userStore = useUserStore();
  if (userStore.info.permissions.includes("*:*:*")) {
    return true;
  }
  const dealAuth: any = (e: any) => {
    // 字符串类型,判断权限
    if (typeof e === 'string') {
      return !e || userStore.info.permissions.includes(e);
    }
    // 对象类型(单属性对象), 对象key可为and | or 
    else {
      // key为and时, value条件须全部符合条件 
      if (Object.keys(e)[0] === 'and') {
        return (Object.values(e)[0] as any[])?.every((x: any) => dealAuth(x));
      }
      // key为or时, value条件只需符合其中一项
      else {
        return (Object.values(e)[0] as any[])?.some((x: any) => dealAuth(x));
      }
    }
  }
  return dealAuth(auth);
}